<?php
namespace Admin\Controller;
use Admin\Controller;
use Think\Page;
include "Extend/Page";
/**
 * 文章管理
 */
class ListController extends BaseController
{
    /**
     * 信息列表
     * @return [type] [description]
     */
    public function index()
    {
        //获取session
        $sid = session('reorder');
        $key = trim(I('post.key')) ;
        //$fid = trim(I('get.fid'));
        //获取前台下拉菜单的值
//        $reorder = I('reorder');
//        //判断是否为空主要是为了分页 后期维护修改
//        if($reorder == ''){
//            $reorder = $sid;
//        }
        if($key == ""){
            if (!empty($key)) {
            $where['nickname'] = $key;
            }
            $model = D('List');
        }else{

              $model = M('List');
            if (filter_var($key, FILTER_VALIDATE_EMAIL)||is_numeric($key)) {
                if(is_numeric($key)){
                    $array['phone'] = trim($key);
                }else{
                    $array['username'] = trim($key);
                }
                $list = M('member')->where($array)->find();
                $where['fid'] = $list['id'];
                $model = $model->where($where);

            } else {

                $where['nickname'] = trim($key);
                $model = $model->where($where);
            }
        }
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
       $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        //不清楚这个意思具体是做什么用的
        if ($status == 1) {
            $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id asc')->select();
        } else {
            if($reorder == "1"){
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('xznum desc')->select();
            }else if($reorder == "2"){
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('xznum asc')->select();
            }else{
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
            }

        }
        //把下拉菜单里的value存到session里 后期修改
       // session('reorder',$reorder);
        $this->assign('model', $post);
        $this->assign('page',$show);
        $this->assign('lengths',$count);
      //  $this->assign('key',$key);

        $this->display();
    }

	
	 /**
     * yyy推广列表
     * @return [type] [description]
     */
    public function center()
    {
        //获取session
        $key = trim(I('post.key')) ;
		$where['status'] = '0';
		$where['istype'] = "<>1";
        if($key == ""){
            if (!empty($key)) {
            $where['nickname'] = $key;
            }
            $model = D('List');
        }else{
              $model = M('List');
            if (filter_var($key, FILTER_VALIDATE_EMAIL)||is_numeric($key)) {
                if(is_numeric($key)){
                    $array['phone'] = trim($key);
                }else{
                    $array['username'] = trim($key);
                }
                $list = M('member')->where($array)->find();
                $where['fid'] = $list['id'];
                $model = $model->where($where);

            } else {

                $where['nickname'] = trim($key);
                $model = $model->where($where);
            }
        }
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
       $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
		$post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
		$hebing = M("Hebing");
		if(!empty($post)){
			foreach ($post as $k => $val) {
				$hbinfo = $hebing->where(array('appid' => $val['id']))->find();
				if(!empty($hbinfo)){
					$info = $model->where(array('id' => $hbinfo['toaddid'],'fid'=>$hbinfo['fid']))->find();
					if($info){
						$post[$k]['ipnum'] = $val['ipnum']+$info['ipnum'];
					}
				}
			}
		}
        //把下拉菜单里的value存到session里 后期修改
       // session('reorder',$reorder);
        $this->assign('model', $post);
        $this->assign('page',$show);
        $this->assign('lengths',$count);
        $this->assign('key',$key);
        $this->display();
    }
	
	/**
     * yyy推广列表
     * @return [type] [description]
     */
    public function iplist()
    {
        $appid = trim(I('get.id')) ;
		$hebing = M("Hebing");	
		$where =array();
		$hbinfo = $hebing->where(array('appid' => $appid))->find();
		if(!empty($hbinfo)){
			$appid  = $appid.",".$hbinfo['toaddid'];
			$where['appid'] = array("in",$appid);
		}else{
			$where['appid'] = $appid;
		}
		$model = M('Ipnum');
		
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
       $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
		$post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
		$hebing = M("Hebing");
		if(!empty($post)){
			foreach ($post as $k => $val) {
			
			$post[$k]['num'] = $k +1;
            $post[$k]['add_time'] = date('Y-m-d h:i:s', $val['add_time']);
			}
		}
        $this->assign('model', $post);
        $this->assign('page',$show);
        $this->assign('lengths',$count);
        $this->display();
    }

    /**
     * 查询列表
     * @return [type] [description]
     */
    public function chaxun($key="")
    {
        if($key == ""){
            $model = D('List');
        }else{
            $where['udid'] = array('like',"%$key%");
            $model = D('List');
        } 
        
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('model', $post);
        $this->assign('page',$show);
        $this->status = I('status');
        $this->display();     
    }

    /**
     * 查询列表
     * @return [type] [description]
     */
    public function memlist(){
        if (IS_GET) {
            $uid = I('get.uid');
            $status = I('get.status');
            if (!empty($uid) && !empty($status)) {
                $model = M('List');
                $where['uid'] = $uid;
                $where['status'] = $status;
               
                $count  = $model->where($where)->count();// 查询满足要求的总记录数
                $Page = new \Extend\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
                $show = $Page->show();// 分页显示输出
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
                
                $this->assign('model', $post);
                $this->assign('page',$show);
                $this->status = $status;
                $this->display('chaxun'); 
            }
        }
    }

    /**
     * 添加文章
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("List");
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($tid = $model->add()) {
                    $where['id'] = $tid;
                    $model-> where($where)->setField('web','index.php/appipa/listll/xxid/'.$tid.'.html');
                    $this->success("添加成功", U('list/index'));
                } else {
                    $this->error("添加失败");
                }
            }
        }
    }

    public function addzd()
    {
            //如果用户提交数据
            $model = M("List");
            $data['create_time'] = time();
                if ($tid = $model->data($data)->add()) {
                    $where['id'] = $tid;
                    $model-> where($where)->setField('web','index.php/appipa/listll/xxid/'.$tid.'.html');
                    $model-> where($where)->setField('plist',$tid);
                    $this->success("添加成功", U('list/index'));
                } else {
                    $this->error("添加失败");
                }
    }

    /**
     * 更新文章信息
     * @param  [type] $id [文章ID]
     * @return [type]     [description]
     */
    public function update($id)
    {
        //默认显示添加表单
        if (!IS_POST) {
            $model = M('List')->where('id='.$id)->find();
            $this->assign('post',$model);
            $this->display();
        }
        if (IS_POST) {

            $data['shorturl'] = $_POST['shorturl'];
            $_POST['web']="/a/".$_POST['shorturl'];
            $where['id'] = $_POST['id'];
            $model = M("List");
            $copy  =$model->where($where)->find();
            $list = $model->where($data)->find();

            if($list&&$list['id']!=$copy['id']){
                $this->error('下载地址重复请从新输入');
            }
            if (!$model->create()) {
                $this->error($model->getError());
            }else{
                if ($model->save()) {
                    $this->success("更新成功", U('list/index'));
                } else {
                    $this->error("更新失败");
                }        
            }
        }
    }

    /**
     * 更新文章信息
     * @param  [type] $id [文章ID]
     * @return [type]     [description]
     */
    public function cx_update($id)
    {
        //默认显示添加表单
        if (!IS_POST) {
            $model = M('List')->where('id='.$id)->find();
            $this->assign('post',$model);
//            $this->status = $status;
            $this->display();
        }
        if (IS_POST) {
            $model = D("List");
            if (!$model->create()) {
                $this->error($model->getError());
            }else{
                if ($model->save()) {
                    $this->success("更新成功", U('post/chaxun'));
                } else {
                    $this->error("更新失败");
                }        
            }
        }
    }

    /**
     * 删除文章
     * @param  [type] $id [description]
     * @return [type]     [description]
    */ 
    public function delete($id)
    {
        if (empty($id)) {
            $this->error("不能提交空值!");
        }
        //解析参数
        $id=$this->jiexi($id);
        $member = M("List");
        //查询 list表中的数据 返回数组格式
        $result = $member->where("id in($id)")->field()->select();



        $post = M('jubao');
        //根据id查询jubao表中的数据返回id
        $hobby = $post->where("appid in($id)")->field('id')->select();
            //循环举报表中的数据id进行循环操作
        foreach ($hobby as $k => $val) {

            $map['id'] = $val['id'];
            unlink('./Public/appipa/'.$val['id'].'.plist');
            unlink('./Public/png/'.$val['id'].'.png');
            //不知道啥用

            //删除操作 举报信息表的数据
            $res = $post->where($map)->delete();
        }
        //删除list表中的数据

        $imgs= $member->where("id in($id)")->field('zxlogo')->select();


        $memenum = $member->where("id in($id)")->delete($id);


        if($memenum){
            $memb = M('member');
            //循环list数组中的内容 取出 id 和 fid
            foreach ($imgs as $key=>$val){

                unlink('./Public/icon/'.$val['zxlogo'] );
            }
            foreach ($result as $index => $inde) {
                $val['fid']= $inde['fid'];
                //取出list表中的个数
                $lists = $member->where($val)->count();
                $data['id'] = $inde['fid'];
               $data['appnum'] = $lists;
               //修改操作
               $save = $memb->data($data)->save($data);

          }

            $this->success("删除成功",  U('List/index'));
        }else{
            $this->error("删除失败",  U('List/index'));
        }
    }
    
	 /**
     * yyy 删除ip数据
     * @param  [type] $id [description]
     * @return [type]     [description]
    */ 
    public function ipdelete($id)
    {
        if (empty($id)) {
            $this->error("不能提交空值!");
        }
        //解析参数
        $id=$this->jiexi($id);
        $member = M("List");
		//循环ip表中的数据id进行循环操作
		$ipnum = M('ipnum');
        //查询 list表中的数据 返回数组格式
        $result = $ipnum->where("appid in($id)")->field()->select();
		
        $post = M('hebing');
        //根据id查询jubao表中的数据返回id
        $hobby = $post->where("appid in($id)")->field('toaddid')->select();
		
		if(!$result && !$hobby){
			$this->error("推广数据为空！",  U('List/center'));
		}
        
		if(!empty($hobby)){
			foreach ($hobby as $k => $val) {
				$map['appid'] = $val['toaddid'];
				//删除操作ip表的数据
				$res = $ipnum->where($map)->delete();
				//更新点击值
				$maps['id'] = $val['toaddid'];
				$data['ipnum'] = '0';
				$istype = $member->where($maps)->save($data);	
			}
		}
        $data['ipnum'] = '0';
        //删除ip表中的数据
        $imgs= $member->where("id in($id)")->save($data);
        $memenum = $ipnum->where("appid in($id)")->delete();
        if($memenum){
            $this->success("删除成功",  U('List/center'));
        }else{
            $this->error("删除失败",  U('List/center'));
        }
    }
	/**
     * yyy 删除ip数据
     * @param  [type] $id [description]
     * @return [type]     [description]
    */ 
    public function newipdelete($id)
    {
        if (empty($id)) {
            $this->error("不能提交空值!");
        }
        //解析参数
        // $id=$this->jiexi($id);
        $member = M("List");
        $ipnum = M("ipnum");
		$map['id'] = $id;
		//根据id查询ipnum表中的数据返回appid
        $info = $ipnum->where($map)->find();
		$where2['id'] = $info['appid'];
		$result = $member->where($where2)->setDec('ipnum',1);
        if($result){
		   //删除操作ip表的数据
			$res = $ipnum->where($map)->delete();
			if($res){
				$this->success("删除成功");
			}else{
				$this->error("删除失败");
			}
	    }
    }
    /**
     * 删除文章
     * @param  [type] $id [description]
     * @return [type]     [description]
     
    public function cx_delete($id,$status)
    {
        //$this->error("禁止删除");
        $model = M('post');
        $map['status']  = array('in','1,2,3');
        $map['id'] = $id;
        $result = $model->where($map)->setInc('status',3);
        //$result = $model->where("id=".$id)->delete();
        if($result){
            $this->success("删除成功", U('post/chaxun'));
        }else{
            $this->error("删除失败");
        }
    }
    */
    /**
     * 批量转移到待处理
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function plmove() {
        
        if (IS_POST) {
            $hobby = $_POST['hobby'];
            if (empty($hobby)) {
                $this->error("不能提交空值!");
            }
            $post = M('List');
            $dd_count = $post->where(array('status'=>'2'))->count();
            if (($dd_count + count($hobby)) > 100) {
                $this->error("待处理信息数量不能超过100条!");
            }
            
            foreach ($hobby as $k => $val) {
                $post->where(array('id'=>$val))->setField('status','2');
            }
            $this->success("转移成功", U('post/index',array('status'=>'2')));
        } else {
            $this->error("请不要恶意提交");
        }
    }

    /**
     * 批量更新
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function plupdate(){
        if (IS_POST) {
            $hobby = $_POST['hobby'];
            if (empty($hobby)) {
                $this->error("不能提交空值!");
            }
            $web = I('web'.$hobby[0]);
            $pid = I('pid'.$hobby[0]);
            if (empty($web) || empty($pid)) {
                $this->error("ID为".$hobby[0]."的批号与网址不能为空!");
            }
            $post = M('Post');
            foreach ($hobby as $k => $val) {
                $post->where(array('id'=>$val))->setField(array('web'=>$web,'pid'=>$pid));
            }
            $this->success("更新成功", U('post/index',array('status'=>$_POST['status'])));
        } else {
            $this->error("请不要恶意提交");
        }
    }

    /**
     * 批量移动到已完成
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function complate() {

        $post = M('Post');
        $where['status'] = 2;
        $where['pid'] = array('neq','');
        $where['web'] = array('neq','');
        $result = $post->where($where)->select();
        if (!empty($result)) {
            foreach ($result as $key => $value) {
                $post->where(array('id'=>$value['id']))->setField(array('status'=>3,'update_com'=>time()));
            }
        }
        $this->success("移动成功", U('post/index',array('status'=>'3')));
    }

    /**
     * 批量导出
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function export() {
        $post = M('Post');
        $result = $post->where(array('status'=>'2'))->select();
        if (empty($result)) {
            $this->error("暂无数据");
        }
        Header( "Content-type:application/octet-stream "); 
        Header( "Accept-Ranges:bytes "); 
        header( "Content-Disposition:attachment;filename=export.txt"); 
        header( "Expires:0"); 
        header( "Cache-Control:must-revalidate,post-check=0,pre-check=0"); 
        header( "Pragma:public"); 
        
        foreach ($result as $k => $val) {
            echo $val['udid'].'  '.$val['rid']."\n";
        }
    }

    /**
     * 删除已完成的超过365天的用户对应数据
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delmem() {
        $this->error("禁止删除");
        $post = M('Post');
        $result = $post->where(array('status'=>3))->select();
        $num = 0;
        foreach ($result as $key => $value) {
            $gq_num = ($value['update_com'] -$value['create_new'])/86400;
            if ($gq_num > 365) {
                $result = $post->where("id=".$value['id'])->delete();
                $num = $num +1 ;
            }
        }

        if($result){
            $this->success("成功清除".$num."条数据", U('index/index'));
        }else{
            $this->error("清除失败");
        }
      
    }

    /**
     * 还原
     * @param $id  post id
     * @param $status  操作函数
     */
    public function huany($id,$status){


        $model = M('List');
        $map['status']  = array('in','0,1,2,3');
        $map['id'] = $id;
        if($status ==3){
        $result = $model->where($map)->setDec('status',3);
        //$result = $model->where("id=".$id)->delete();
        if($result){
            $this->success("还原成功", U('list/index',array('status'=>'0')));
        }else{
            $this->error("还原失败");
        }
        }else if($status ==0){
               $result = $model->where($map)->setInc('status',3);
               if($result){
                   $this->success("下架成功", U('list/index',array('status'=>'0')));
               }else{
                   $this->error("下架失败");
               }
           };

    }

    /**
     * 批量还原
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function alldel() {
        
        if (IS_POST) {
            $hobby = $_POST['hobby'];
             $sss= $_POST['quanxuan'];
            if (empty($hobby)) {
                $this->error("不能提交空值!");
            }
            $post = M('List'); 
            foreach ($hobby as $k => $val) {

                $map['id'] = $val;
                $res = $post->where($map)->delete();
                unlink('./Public/appipa/'.$val.'.plist');
                unlink('./Public/png/'.$val.'.png');

                if ($result['uptype'] == 2) {
                    $uptxt = $post->where(array('id'=>$val))->getFiled('uptxt');
                    unlink($uptxt);
                }
            }
            
            if($res){
                    //unlink('./Public/uploads/'.$info['uptxt']);    
                $this->success("批量删除成功", U('List/index'));
            }else{
                $this->error("删除失败", U('List/index'));
            }

        } else {
            $this->error("请不要恶意提交");
        }
    }

    //自定义批量删除
    public function delAll(){
        //实例化一个对象
        $list  = M("List");
        //获取前台传过来的参数
        $shanchu = I('shanchu');
        //解析参数
        $arr = explode(",",$shanchu);
        //如果有多余字符去掉
        $arr = array_merge(array_diff($arr, array("undefined")));
        $arr = array_merge(array_diff($arr, array("on")));

        if(is_array($arr)){
            $where = implode(',',$arr);
        }else{
            $where = 'id='.$arr;
        }


        $result = $list->delete($where);

        //redirect(U('index/index'));
        if($result) {
            $this->success("批量删除成功", U('List/index'));
          //  $this->ajaxReturn($result,"删除成功！");
        }else{
          $this->error("删除失败", U('List/index'));
         // $this->ajaxReturn($result,"删除失败！");
        }


    }

    function jiexi($id){
        //解析参数
        $arr = explode(",",$id);
        //如果有多余字符去掉
        $arr = array_merge(array_diff($arr, array("undefined")));
        $arr = array_merge(array_diff($arr, array("on")));

        if(is_array($arr)){
            return  $id = implode(',',$arr);
        }else{
            return   $id = 'id='.$arr;
        }
    }
    function selectType(){
    	//获取session
        $cid = session('cunchu');
        $key = trim(I('post.key')) ;
        $fid = trim(I('get.fid'));
        //获取前台下拉菜单的值
        $chuncu  = I('cunchu');
        $reorder  = I('reorder');
        //判断是否为空主要是为了分页 后期维护修改

        if($chuncu == ''){
            $chuncu = $cid;
        }
        if($key == ""){
            if (!empty($fid)) {
                $where['fid'] = $fid;
            }
            $where['isgg'] =$chuncu;
            $model = D('List');

        }else{
            $where['nickname'] = trim($key);
            $model = M('List');
        }

        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        //不清楚这个意思具体是做什么用的
        if ($status == 1) {
            $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id asc')->select();
        } else {
            if($reorder == "1"){
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('xznum desc')->select();
            }else if($reorder == "2"){
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('xznum asc')->select();
            }else{
                $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id desc')->select();
            }

        }
        //把下拉菜单里的value存到session里 后期修改
       // session('reorder',$reorder);
        session('cunchu',$chuncu);
        $this->assign('model', $post);
        $this->assign('page',$show);
        $this->assign('lengths',$count);
        $this->display("List/index");
    }

}
